home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15825 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  84 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How do I put DIFFERENT classes in the same linked list?
  5. Date: 8 Apr 1996 10:22:54 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4kapdu$ocq@grimsel.zurich.ibm.com>
  8. References: <4ka938$bj6@wintermute.ecs.fullerton.edu>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. In <4ka938$bj6@wintermute.ecs.fullerton.edu>, grosin@titan (Gil Rosin) writes:
  14. >I'm trying to write a program where I need to maintain a linked list of
  15. >various items. Each item is a class, but each item is different from another
  16. >item. Let me give an example to clarify:
  17. >
  18.  
  19. You're writing a heterogeneous collection class - a collection of mixed
  20. objects: the classic example is a fruitbowl which can contain apples,
  21. oranges, pears... In your case you have a library which can contain 
  22. books, magazines, records, CDs or whatever. Contrast this with a 
  23. bookshelf which can contain only books - a homogeneous collection class.
  24.  
  25. The ingredients of the heterogeneous collection are:
  26.  
  27.    An (Abstract) base class
  28.  
  29.    Instance Classes to store in the collection derived from 'Base'
  30.  
  31.    A container class (such as a list, array etc.)
  32.  
  33. The container class is, normally, also derived from the base class to 
  34. so that you can have collections of collections - this is the next
  35. level of abstraction which you can leave for the moment.
  36.  
  37. The base class describes the type of objects which can be stored:
  38.  
  39. Classes
  40. =======
  41.  
  42. Container:      Bowl                      Library
  43.  
  44. Base:           Fruit                     Publication
  45.  
  46. Instance:       Apple, Orange,..          Book, Magazine,..
  47.  
  48. Given that you can defined your classes, add the methods you need
  49. migrating methods that are common to all dervied (instance) classes to 
  50. the base class. Here's a start for the fruit bowl example...
  51.  
  52. enum FRUIT { APPLE, ORANGE  /* etc. */ };
  53.  
  54. class Fruit
  55.   {
  56.   public:
  57.     virtual FRUIT Type(void) const = 0;
  58.     int Weight(void) const;
  59.   // ,,,
  60.   };
  61.  
  62. class Apple:
  63.   public Fruit
  64.   {
  65.   public:
  66.     FRUITTYPE Type(void) const = 0;
  67.     int Crunchiness(void) const;
  68.   };
  69.  
  70. class Bowl
  71.   {
  72.   public:
  73.     void Add(Fruit &);
  74.     void Delete(Fruit &);
  75.     int Count(void) const;
  76.     // ...
  77.   }
  78.  
  79. Hope that helps...
  80.  
  81. Keith
  82.  
  83.  
  84.